package com.firebird.app.kotlin.stock.ui.free_interset.presenter

import com.firebird.app.kotlin.stock.App
import com.firebird.app.kotlin.stock.base.BasePresenter
import com.firebird.app.kotlin.stock.data.SimpleBean
import com.firebird.app.kotlin.stock.ui.free_interset.contract.FreeIntersetContract
import com.firebird.app.kotlin.stock.ui.free_interset.model.FreeIntersetModel
import com.firebird.app.kotlin.stock.utils.SharedPreferencesUtil
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class FreeIntersetPresenter : BasePresenter<FreeIntersetContract.View?>(), FreeIntersetContract.Presenter {
    private val model: FreeIntersetContract.Model
    override fun getData(guaranteeAmt: String?, bpId: Int, times: Int, bpType: Int, singleRate: String?, bpAround: Int, bpTimeType: Int) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getData(guaranteeAmt, bpId, times, bpType, singleRate, bpAround, bpTimeType)!!.enqueue(object : Callback<SimpleBean?> {
            override fun onResponse(call: Call<SimpleBean?>, response: Response<SimpleBean?>) {
                mView!!.hideLoading()
                mView!!.onSuccess(response.body())
                SharedPreferencesUtil.getInstance(App.context!!)!!.putSP("token", response.headers()["token"])
            }

            override fun onFailure(call: Call<SimpleBean?>, t: Throwable) {
                mView!!.hideLoading()
                mView!!.onError(t)
            }
        })
    }

    init {
        model = FreeIntersetModel()
    }
}